package cn.dnui_yxk801.dnuifood_16110100801.controller;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecordDBOperation {
    private String TABLE_NAME="records";
    private SQLiteDatabase db;
    public RecordDBOperation(RecordSQLiteOpenHelper helper){
        db=helper.getWritableDatabase();
    }
    public void insertData(String tempName) {
        db.execSQL("insert into "+TABLE_NAME+"(name)values('"+tempName+ "')");
        //jdb.close();
    }
    public List queryData(String tempName){
        List list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select id as _id,name from "+TABLE_NAME+" where name like '%"+tempName+"%'order by id desc ",null);
    while (cursor.moveToNext()){
        Map map =new HashMap<>();
        map.put("name",cursor.getString(cursor.getColumnIndex("name")));
        list.add(map);
    }
    //db.close();
    //cursor.close();
    return list;
    }

    public boolean hasData(String tempName){
        Cursor cursor=db.rawQuery("select id as _id,name from "+TABLE_NAME+" where name=?",new String[]{tempName});
        return cursor.moveToNext();
    }

    public void deleteData(){
        db.execSQL("delete from "+TABLE_NAME);
        //db.close();
    }
}
